// const reverseList = function(head) {
//     if (head === null || head.next === null) {
//         return head;
//     }
//     let p = head.next;
//     head.next = null;
//     while (p !== null) {
//         const temp = p.next;
//         p.next = head;
//         head = p;
//         p = temp;
//     }
//     return head;
// }

const reverseList = function(head) {
    if (head === null || head.next === null) {
        return head;
    }
    const newhead = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return newhead;
}
head = [1, 2, 3, 4, 5]
console.log(reverseList(head));